package com.yunfangdata.web.controller;


import com.yunfangdata.domain.Classroom;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IClassroomService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("classroom")
public class ClassroomController {

    @Autowired
    private IClassroomService classroomService;

    @RequestMapping("")
    @RequiresPermissions("classroom:index")
    @PermissionName("教室首页")
    public String index(){
        return "classroom/list";
    }

    @RequiresPermissions("classroom:list")
    @PermissionName("教室列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        System.out.println(classroomService.query(qo));
        return classroomService.query(qo);

    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("classroom:save")
    @PermissionName("教室保存")
    public JsonResult save(Classroom entity){
        JsonResult result = new JsonResult();
        try {
            classroomService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("classroom:update")
    @PermissionName("更新教室")
    public JsonResult update(Classroom entity){
        JsonResult result = new JsonResult();
        try {
        }catch (Exception e){
            classroomService.update(entity);
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }


    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("classroom:changeState")
    @PermissionName("教室状态")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            classroomService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

}
